import React from 'react';
import { FormGroup, DATA_ENTRY } from '../../../../../comps';
import { message } from 'antd';
import { connect } from 'dva';
import moment from 'moment';

class TzBaseInfo extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      baseInfoRef: undefined,
      startDate: undefined,
      endDate: undefined,
    };
  }
  componentDidMount() {
    this.props?.onRef && this.props?.onRef(this);
  }
  UNSAFE_componentWillReceiveProps(nextPorps) {
    if (JSON.stringify(nextPorps.baseInfo) != JSON.stringify(this.props.baseInfo)) {
      this.setState({
        // 处理起息日和到期日在编辑时的禁用逻辑
        startDate: nextPorps.baseInfo.startDate ? moment(nextPorps.baseInfo.startDate).format('YYYY-MM-DD') : undefined,
        endDate: nextPorps.baseInfo.endDate ? moment(nextPorps.baseInfo.endDate).format('YYYY-MM-DD') : undefined,
      });
    }
  }
  //可供外部调用获取投资类基本信息
  getInfo = (callback) => {
    let values = this.state.baseInfoRef.getFormGroupData();
    if (values) {
      values.startDate = values.startDate ? moment(values.startDate).format('YYYY-MM-DD') : '';
      values.endDate = values.endDate ? moment(values.endDate).format('YYYY-MM-DD') : '';
      values.jgbb_txll = values.jgbb_txll != null && values.jgbb_txll != undefined ? values.jgbb_txll / 100 : undefined;
      callback(values);
    } else {
      message.error('请填写完表单再提交！');
      return false;
    }
    // this.state.baseInfoRef.props.form.validateFieldsAndScroll((err,values)=>{
    //     if(err){
    //         message.error('请填写完表单再提交！');
    //         return false;
    //     }else{
    //         values.startDate = values.startDate ? moment(values.startDate).format('YYYY-MM-DD'):'';
    //         values.endDate = values.endDate ? moment(values.endDate).format('YYYY-MM-DD'):'';
    //         callback(values);
    //     }
    // })
  };
  render() {
    const { disableStatus, baseInfo = {}, nonstandarmaintenanceNewM, pageType } = this.props;
    const { dictMapSelect } = nonstandarmaintenanceNewM;
    const disabled = disableStatus;
    return (
      <FormGroup
        autoLayout={true}
        onRef={(ref) => {
          this.state.baseInfoRef = ref;
        }}
        dataSource={[
          {
            dataIndex: 'assetType',
            title: '资产类别',
            type: DATA_ENTRY.SELECT,
            initialValue: baseInfo.assetType || '1',
            disabled: true,
            dataSource: dictMapSelect['INVEST_ASSET_TYPE'] || [],
            rules: [{ required: true, message: '请选择资产类别' }],
          },
          {
            dataIndex: 'tradingcode',
            title: '证券代码',
            type: DATA_ENTRY.INPUT,
            initialValue: baseInfo.tradingcode,
            disabled: pageType == 'edit' ? true : disabled,
            rules: [{ required: true, message: '请输入证券代码' }],
          },
          {
            dataIndex: 'tradingname',
            title: '证券全称',
            type: DATA_ENTRY.INPUT,
            initialValue: baseInfo.tradingname,
            disabled: disabled,
            rules: [{ required: true, message: '请输入证券名称' }],
          },
          {
            dataIndex: 'secuabbr',
            title: '证券简称',
            type: DATA_ENTRY.INPUT,
            initialValue: baseInfo.secuabbr,
            disabled: disabled,
            rules: [{ required: true, message: '请输入证券简称' }],
          },
          {
            dataIndex: 'currency',
            title: '币种',
            type: DATA_ENTRY.SELECT,
            initialValue: baseInfo.currency || 'CNY',
            dataSource: dictMapSelect['INVEST_CURRENCY'] || [],
            disabled: true,
            rules: [{ required: true, message: '请选择币种' }],
          },
          {
            dataIndex: 'startDate',
            title: '起息日',
            type: DATA_ENTRY.SINGLEDATE,
            initialValue: baseInfo.startDate ? moment(baseInfo.startDate) : undefined,
            disableEnd: this.state.endDate ? moment(this.state.endDate).add(1, 'days') : '',
            disabled,
            onChange: (e) => {
              this.props?.changeStartDate && this.props?.changeStartDate(e), this.setState({ startDate: e.format('YYYY-MM-DD') });
            },
            rules: [{ required: true, message: '请选择起息日' }],
          },
          {
            dataIndex: 'endDate',
            title: '到期日',
            type: DATA_ENTRY.SINGLEDATE,
            initialValue: baseInfo.endDate ? moment(baseInfo.endDate) : undefined,
            disableBegin: this.state.startDate ? moment(this.state.startDate).subtract(1, 'days') : '',
            disabled,
            onChange: (e) => {
              this.props?.changeEndDate && this.props?.changeEndDate(e), this.setState({ endDate: e.format('YYYY-MM-DD') });
            },
            rules: [{ required: true, message: '请选择到期日' }],
          },
          {
            dataIndex: 'jgbb_jyltcs',
            title: '交易市场',
            type: DATA_ENTRY.SELECT,
            initialValue: baseInfo?.jgbb_jyltcs,
            allowClear: true,
            disabled,
            dataSource: dictMapSelect['INVEST_JYLTCS'] || [],
            rules: [{ required: true, message: '请选择交易流通场所' }],
          },
          {
            dataIndex: 'jgbb_type',
            title: '票据类型',
            type: DATA_ENTRY.SELECT,
            initialValue: baseInfo?.jgbb_type,
            allowClear: true,
            disabled,
            dataSource: dictMapSelect['INVEST_TYPE'] || [],
          },
          {
            dataIndex: 'jgbb_sl',
            title: '票据数量',
            type: DATA_ENTRY.INPUTNUMBER,
            unit: '张',
            min: 0,
            precision: 0,
            initialValue: baseInfo?.jgbb_sl,
            disabled,
            rules: [
              { pattern: /(^-?(?:\d|\d{0,6})?$)/, message: '字符不能超过6位！' },
              { required: true, message: '请输入数量' },
            ],
          },
          {
            dataIndex: 'jgbb_hjje',
            title: '合计金额',
            type: DATA_ENTRY.INPUTNUMBER,
            unit: '元',
            splitStyle: true,
            min: 0,
            precision: 2,
            initialValue: baseInfo?.jgbb_hjje,
            disabled,
            rules: [
              { pattern: /(^-?(?:\d|\d{0,12})(?:\.\d{0,2})?$)/, message: '字符不能超过15位！' },
              { required: true, message: '请输入合计金额' },
            ],
          },
          {
            dataIndex: 'jgbb_txll',
            title: '贴现利率',
            type: DATA_ENTRY.INPUTNUMBER,
            unit: '%',
            min: 0,
            max: 99.99999,
            precision: 5,
            initialValue: baseInfo?.jgbb_txll ? Number(baseInfo?.jgbb_txll * 100).toFixed(5) : undefined,
            disabled,
            rules: [
              { pattern: /(^-?(?:\d|\d{0,2})(?:\.\d{0,5})?$)/, message: '字符不能超过8位！' },
              { required: true, message: '请输入贴现利率' },
            ],
          },
          {
            dataIndex: 'jgbb_jqsyqx',
            title: '加权剩余期限',
            type: DATA_ENTRY.INPUTNUMBER,
            unit: '天',
            min: 0,
            precision: 2,
            initialValue: baseInfo?.jgbb_jqsyqx,
            disabled,
            rules: [{ pattern: /(^-?(?:\d|\d{0,3})(?:\.\d{0,2})?$)/, message: '字符不能大于等于7位！' }],
          },
          {
            dataIndex: 'jgbb_sfwbzhpj',
            title: '是否为标准化票据',
            type: DATA_ENTRY.SELECT,
            initialValue: baseInfo?.jgbb_sfwbzhpj,
            isTrueFalseJudge: true,
            allowClear: true,
            disabled,
          },
        ]}
      ></FormGroup>
    );
  }
}
export default connect(({ nonstandarmaintenanceNewM }) => ({
  nonstandarmaintenanceNewM,
}))(TzBaseInfo);
